﻿(function() {
    var app = {
        init: function () {
            this.currentUser = window.User.getCurrentUser();
            this.achievements = window.Achievement.getAllAchievements();
            var stageSize = { 
                width: 800, 
                height: 600
            };
            var firstPageSize = {
                width: 630,
                height: 460
            };
            var achievementsPageSize = { 
                width: stageSize.width - 80, 
                height: stageSize.height - 80 
            };
            var firstPagePosition = { 
                x: 20, 
                y: 70
            };
            this.addPage(window.initFirstPage(this, firstPagePosition, firstPageSize), 'firstPage');
            this.addPage(window.initLevelPage(this, firstPagePosition, firstPageSize), 'levelPage');
            this.addPage(window.initAchievementsPage(this, {x:40, y: 40}, achievementsPageSize), 'achievementsPage');
            this.addPage(window.initWrapper(this, {x: 0, y: 0}, stageSize), 'wrapper');
            this.addPage(window.initAddMoneyPage(this, {x:40, y: 40}, achievementsPageSize), 'addMoneyPage');
            this.addPage(window.initHelpPage(this, {x:40, y: 40}, achievementsPageSize), 'helpPage');
            this.addPage(window.initPurchasingsPage(this, {x:40, y: 40}, achievementsPageSize), 'purchasingsPage');
            this.moveToTop('wrapper');
            this.moveToTop('firstPage');
            this.moveToTop('purchasingsPage');
        },
        addPage: function (page, name) {
            if (!this.pages) {
                this.pages = [];
            }
            this.pages.push({ name: name, page: page });
            document.getElementById(name).style.zIndex = this.pages.length;
        },
        moveToTop: function (pageName) {
            var pageEl = document.getElementById(pageName);
            for (var i in this.pages) {
                var item = this.pages[i];
                var el = document.getElementById(item.name);
                if (el.style.zIndex > pageEl.style.zIndex) {
                    el.style.zIndex--;
                }
            }
            pageEl.style.zIndex = this.pages.length;
            this.draw();
        },
        moveToBottom: function (pageName) {
            var pageEl = document.getElementById(pageName);
            for (var i in this.pages) {
                var item = this.pages[i];
                var el = document.getElementById(item.name);
                if (el.style.zIndex < pageEl.style.zIndex) {
                    el.style.zIndex++;
                }
            }
            pageEl.style.zIndex = 1;
            this.draw();
        },
        draw: function () {
            for (var p in this.Pages) {
                this.pages[p].page.draw();
            }
        }
    };
    app.init();
})();